package com.pulse.base.controller;

import com.pulse.base.domain.CircuitMeasure;
import com.pulse.base.service.ICircuitMeasureService;
import com.pulse.common.annotation.Log;
import com.pulse.common.core.controller.BaseController;
import com.pulse.common.core.domain.AjaxResult;
import com.pulse.common.core.page.TableDataInfo;
import com.pulse.common.enums.BusinessType;
import com.pulse.common.exception.ServiceException;
import com.pulse.common.utils.SecurityUtils;
import com.pulse.common.utils.poi.ExcelUtil;
import com.pulse.common.utils.sor.Sor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 光路监测Controller
 *
 * @author tongwei
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/base/circuitMeasure")
public class CircuitMeasureController extends BaseController {

    @Autowired
    private ICircuitMeasureService circuitMeasureService;

    /**
     * 查询光路监测列表
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:list')")
    @GetMapping("/list")
    public TableDataInfo list(CircuitMeasure circuitMeasure)
    {
        startPage();
        List<CircuitMeasure> list = circuitMeasureService.selectCircuitMeasureList(circuitMeasure);
        return getDataTable(list);
    }

    /**
     * 导出光路监测列表
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:export')")
    @Log(title = "光路监测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CircuitMeasure circuitMeasure)
    {
        List<CircuitMeasure> list = circuitMeasureService.selectCircuitMeasureList(circuitMeasure);
        ExcelUtil<CircuitMeasure> util = new ExcelUtil<CircuitMeasure>(CircuitMeasure.class);
        util.exportExcel(response, list, "曲线数据");
    }

    /**
     * 导出sor文件
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:export')")
    @Log(title = "光路监测", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSor")
    public void exportSor(HttpServletResponse response, CircuitMeasure circuitMeasure)
    {
        try {
            circuitMeasureService.exportSor(response,circuitMeasure);
        } catch (Exception e) {
            throw new ServiceException("导出失败");
        }
    }

    /**
     * 获取光路监测详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:query')")
    @GetMapping(value = "/{measureId}")
    public AjaxResult getInfo(@PathVariable("measureId") Long measureId)
    {
        return success(circuitMeasureService.selectCircuitMeasureByMeasureId(measureId));
    }

    /**
     * 获取参考曲线
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:query')")
    @GetMapping("/getDefaultMeasure/{measureId}")
    public AjaxResult getDefaultMeasure(@PathVariable("measureId") Long measureId)
    {
        return AjaxResult.success(circuitMeasureService.getDefaultMeasure(measureId));
    }

    /**
     * 删除光路监测
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:remove')")
    @Log(title = "光路监测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{measureIds}")
    public AjaxResult remove(@PathVariable Long[] measureIds)
    {
        return toAjax(circuitMeasureService.deleteCircuitMeasureByMeasureIds(measureIds));
    }

    /**
     * 设置参考曲线
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:edit')")
    @Log(title = "光路监测", businessType = BusinessType.UPDATE)
    @PutMapping("/setDefaultMeasure/{measureId}")
    public AjaxResult setDefaultMeasure(@PathVariable("measureId") Long measureId)
    {
        return toAjax(circuitMeasureService.setDefaultMeasure(measureId));
    }

    /**
     * 查看曲线
     */
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:list')")
    @GetMapping("/viewCurve/{measureIds}")
    public AjaxResult viewCurve(@PathVariable Long[] measureIds)
    {
        return AjaxResult.success(circuitMeasureService.viewCurve(measureIds));
    }

    /**
     * 导入sor文件
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "光路监测", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:circuitMeasure:import')")
    @PostMapping("importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        String operName = SecurityUtils.getUsername();
        String message = "";
        try {
            message = circuitMeasureService.importSor(file, updateSupport, operName);
        }catch (ServiceException e){
            return AjaxResult.error(e.getMessage());
        }
        return success(message);
    }
}
